# coding: utf-8
import requests
import parsel
import urllib3
import os

urllib3.disable_warnings()
requests.packages.urllib3.disable_warnings()

def get_info(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Linux; Android 13; MEIZU 18s Build/TKQ1.221114.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.5563.116 Mobile Safari/537.36"
    }
    response = requests.get(url=f"{url}", headers=headers, verify=False)
    response.encoding='utf-8'
    print(response.text)
    selector1 = parsel.Selector(response.text)
    header_contents = selector1.css('.article_content table')
    page_content = '<html lang="zh-CN"><head></head><body>{}</body></html>'
    table_content = ''
    
    index = 1
    for table in header_contents:
        table_file_name = f"question_list_data_{index}.txt"
        data_content = ''
        trs = table.css(' tbody > tr')
        index += 1
        for tr in trs:
            # print(tr.get())
            item = tr.css('tr > td:nth-child(1) > span::text')
            question = tr.css('tr > td:nth-child(2) a::text')
            question_csdn_url = tr.css('tr > td:nth-child(2) a::attr(href)')
            kaodian = tr.css('tr > td:nth-child(3) > span::text')
            question_url = tr.css('tr > td:nth-child(4) a::attr(href)')
            print(item.get(), question.get(), kaodian.get(), question_url.get(), question_csdn_url.get())
            if item.get() or question.get() or kaodian.get() or question_url.get() or question_csdn_url.get():
                data_content += f"{item.get()},{question.get()},{kaodian.get()},{question_url.get()},{question_csdn_url.get()}\n"
        # print(oj.get())
        # table_content += f'{table.get()}<br/>'
        with open(file=table_file_name, mode='w+', encoding='utf-8') as f:
                  f.write(data_content)
    # with open(file='csdn_html.html', mode='w+', encoding='utf-8') as f:
    #     f.write(page_content.format(table_content))
    
def get_question_content(question_url, file_name, question_page_url):
    headers = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 13; MEIZU 18s Build/TKQ1.221114.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.5563.116 Mobile Safari/537.36"
}   
    if question_url == 'None':
        question_title = ''
        title = ''
        question_content = f'<a href="{question_page_url}">试题页面</a>'
    else:
        response = requests.get(url=f"{question_url}", headers=headers, verify=False)
        response.encoding='utf-8'
        # print(response.text)
        selector1 = parsel.Selector(response.text)
        question_title = selector1.css('#panel > div.main > div.row > div.medium-9.columns > div:nth-child(1) > div.section__header > h1::text')
        question_content = selector1.css('#panel > div.main > div.row > div.medium-9.columns > div:nth-child(2) > div > div > div.section__body.typo.richmedia').get()
        title = question_title.get().strip()
        print(title)
        print(question_content)
    page_content = '<html lang="zh-CN"><head></head><body>{}{}</body></html>'
    with open(file=f'{file_name}.html', mode='w+', encoding='utf-8') as f:
        f.write(page_content.format(title, question_content))

def get_quetsion_from_file():
    source_file_name = "question_list_data_{}.txt"
    dir_name = "question-part-{}"
    for i in range(12):
        if i <= 10:
            continue
        if not os.path.exists(f'{dir_name.format(i+1)}'):
            os.mkdir(dir_name.format(i+1))
        lines = open(file=source_file_name.format(i)).read().split('\n')
        print(lines)
        for line in lines:
            data = line.split(',')
            print(data)
            if len(data) < 2:
                continue
            file_name = f'{dir_name.format(i+1)}/{data[0]}.{data[1]}'
            question_url = data[3]
            if question_url != None:
                get_question_content(file_name=file_name, question_url=question_url, question_page_url=data[4])
    
    
if __name__ == "__main__":
    url = "https://fcqian.blog.csdn.net/article/details/127914382"
    # get_info(url)
    question_url = 'https://hydro.ac/d/HWOD2023/p/OD445'
    # get_question_content(question_url=question_url)
    get_quetsion_from_file()